const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // slide
    value:0,
    maxvalue:'',




    page:1,
    limit:10,

    list:[],
    music:'',
    number:'-1',


    starttime:'00:00',
    endtime:''
  },


  // 防止冒泡
  mp:function(){
    return false
  },
  // 点击进度条跳动
  sliderchange(event){
    console.log(event.detail)
    this.backmusic.seek(event.detail.value)
  },
  sliderchanging(event){
    console.log(event.detail)
    this.backmusic.seek(event.detail.value)
  },

  // 开始播放
  audioPlay(e){
    console.log(e)
    let that = this

    if(!e.currentTarget.dataset.music){
//    wx.showToast({
//      title:'音频地址不正确',
//      icon:'none',
//      duration:2000
//    })
      return false
    }

    that.setData({
      music:e.currentTarget.dataset.music,
      number:e.currentTarget.dataset.index,
    })
    that.music()
    that.backmusic.onTimeUpdate(()=>{
      //console.log(that.backmusic.duration)  //总时长
      //console.log(that.backmusic.currentTime)//进行
        // 计算时长
        let sumtime = that.backmusic.duration;
        let newtime = that.backmusic.currentTime
        let width = newtime / sumtime * 100 + '%'
        // 总时间
        let sumtimem = parseInt(sumtime / 60);
        let sumtimes = parseInt(sumtime % 60);
        if (sumtimem < 10) {
          sumtimem = "0" + sumtimem
        }
        if (sumtimes < 10) {
          sumtimes = "0" + sumtimes
        }
        let newtimem = parseInt(newtime / 60);
        let newtimes = parseInt(newtime % 60);


        if (newtimem < 10) {
          newtimem = "0" + newtimem
        }
        if (newtimes < 10) {
          newtimes = "0" + newtimes
        }

        let end = sumtimem + ':' + sumtimes;
        let start = newtimem + ':' + newtimes
        console.log(end)
        console.log(start)

      that.setData({
        value:that.backmusic.currentTime,
        maxvalue:that.backmusic.duration,
        starttime:start,
        endtime:end
      })
    })

    // 循环播放
    that.backmusic.onEnded(function(){
      that.music()
    })

  },
  // 停止播放
  audioPause(){
    let that = this
    that.setData({
      number:'-1',
    })
    that.backmusic.pause()
  },



  // 创建背景音乐
  music(){
    let that = this
    that.backmusic = wx.getBackgroundAudioManager()
    that.backmusic.src =  that.data.music
    that.backmusic.title="测试"
  },


  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    let page = that.data.page
    page++
    that.setData({
      page:page
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },


  getlist(){
    let that = this
    wx.showLoading({
      title:'加载中'
    })
    wx.getStorage({
      key:'puser',
      success(e){
        console.log(e);
        let setdata = {
          token:e.data.token,
          page:that.data.page,
          limit:that.data.limit
        }
        console.log(setdata);
        wx.request({
          url:app.api + app.pgoods,
          method:'POST',
          data:setdata,
          success(res){
            wx.hideLoading()
            console.log(res)
            if(res.data.code == 0){
              if(that.data.page == 1){
                if(!res.data.data.list.length){
                  wx.showToast({
                    title:'暂无数据',
                    icon:'none',
                    duration:2000
                  })
                }
                that.setData({
                  list:res.data.data.list
                })
              }else if(res.data.data.list.length){
                let oldlist = that.data.list
                let newlist = oldlist.concat(res.data.data.list)
                that.setData({
                  list:newlist
                })
              }else{
                wx.showToast({
                  title:'没有更多了',
                  icon:'none',
                  duration:2000
                })
              }
            }
            
          }
        })
      }
    })
    
  }
})